package com.mew.chess.core.game.pokdeng.game.task;

import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.pokdeng.game.PokDengGameManager;
import com.mew.chess.core.game.pokdeng.game.PokDengSeatManager;
import com.mew.chess.core.game.pokdeng.game.PokDengTableManager;
import com.mew.chess.core.game.pokdeng.game.common.PokDengCardType;
import com.mew.chess.core.game.pokdeng.game.common.PokDengConstantValue;
import com.mew.chess.core.game.pokdeng.game.common.PokDengLinkType;
import com.mew.chess.core.game.pokdeng.game.common.PokDengPushCMDMapType;
import com.mew.chess.core.game.pokdeng.game.core.CardUtil;
import com.mew.chess.core.game.pokdeng.game.message.CardMessage;
import com.mew.chess.core.game.pokdeng.game.message.PushBetMessage;
import com.mew.chess.core.game.pokdeng.game.message.PushDealCardMessage;
import com.mew.chess.core.game.pokdeng.game.message.UserMessage;
import com.mew.common.dc.redis.AucRedisService;
import com.mew.common.enm.UserType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dylin
 * @create 2021/4/29
 */
@Component
@Slf4j
public class PokDengDealCardTask {
    @Autowired
    private AucRedisService aucRedisService;

    public boolean dealCard(int roomId,int tableId){
        PokDengTableManager table = PokDengGameManager.getInstance().getController(roomId).getTableManager(tableId);

        if(CardUtil.redCardDeviceConfig()){
            log.info(" ****** PokDeng room has control");
            table.setControl(true);
        }

        for (PokDengSeatManager seat : table.getSeatMap().values()) {
            if(null!=seat.getUser()&&seat.getUser().getBet()==0){
                seat.getUser().bet(table.getBetArrays()[0]);
                PushBetMessage message = table.getPushBetMessage();
                message.setGid(seat.getUser().getGid());
                message.setSeatNo(seat.getSeatNo());
                message.setBet(table.getBetArrays()[0]);
                if(seat.getUser().getUserType() != UserType.Robot.code()){
                    aucRedisService.chessBetLock(seat.getUser().getUserId(), PokDengConstantValue.OVER_TIME); //参与
                }
                ChessPushMessageUtil.broadcastMessage(table.getOnlineUser().values(), PokDengPushCMDMapType.BetInfoMessage.code(),message);
            }
        }
        table.getCardLib().addAll(CardUtil.getShuffleCardLib());
        //------start game-----
        UserMessage banker=table.getBanker().getUser();
        banker.getCards().add(CardUtil.deal(table.getCardLib(),table.getBanker().getSeatNo(),table.isControl(),banker.getCards().size()));
        banker.getCards().add(CardUtil.deal(table.getCardLib(),table.getBanker().getSeatNo(),table.isControl(),banker.getCards().size()));
        CardUtil.computeCard(banker);
        for (PokDengSeatManager seat : table.getSeatMap().values()) {
            UserMessage user=seat.getUser();
            if(user!=null&&user.getBet()>0){
                user.getCards().add(CardUtil.deal(table.getCardLib(),seat.getSeatNo(),table.isControl(),seat.getUser().getCards().size()));
                user.getCards().add(CardUtil.deal(table.getCardLib(),seat.getSeatNo(),table.isControl(),seat.getUser().getCards().size()));
                CardUtil.computeCard(user);
            }
        }
        table.changeLink(PokDengLinkType.DealCard.code(), PokDengConstantValue.DEAL_CARD_TIME);
        sendPaymentMsg(table);
        return banker.getCardType()== PokDengCardType.Major.code();
    }
    private void sendPaymentMsg(PokDengTableManager table){
        PushDealCardMessage pushDealCardMessage=table.getPushDealCardMessage();
        pushDealCardMessage.setStartIndex(table.getAskStartSeat()+1);
        pushDealCardMessage.setBanker(CardMessage.buildByUser(table.getBanker()));
        List<CardMessage> cards=new ArrayList<>();
        for (PokDengSeatManager seat : table.getSeatMap().values()) {
            if(seat.getUser()!=null&&seat.getUser().getCards().size()>0){
                cards.add(CardMessage.buildByUser(seat));
            }
        }
        pushDealCardMessage.setCards(cards);
        ChessPushMessageUtil.broadcastMessage(table.getOnlineUser().values(), PokDengPushCMDMapType.DealCardMessage.code(),pushDealCardMessage);
    }
}
